package com.atguigu.guli.service.edu.controller.admin;


import com.atguigu.guli.common.base.result.R;
import com.atguigu.guli.service.edu.entity.Teacher;
import com.atguigu.guli.service.edu.entity.Tests;
import com.atguigu.guli.service.edu.entity.form.CourseInfoForm;
import com.atguigu.guli.service.edu.entity.vo.CourseQueryVo;
import com.atguigu.guli.service.edu.entity.vo.CourseVo;
import com.atguigu.guli.service.edu.service.ITestsService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杨婉莹
 * @since 2021-03-13
 */
@CrossOrigin //跨域
@RestController
@RequestMapping("admin/edu/tests")
public class TestsController {
    @Autowired
    private ITestsService testsService;

//    @GetMapping("list")
//    public List<Tests> listAll(){
//        return testsService.list();
//    }

    @ApiOperation(value="所有测试列表")
    @GetMapping("list")
    public R listAll(){
        List<Tests> list = testsService.list(null);
        return R.ok().data("items", list).message("获取测试列表成功");
    }

    @ApiOperation(value = "获取测试")
    @GetMapping("get/{id}")
    public R get(
            @ApiParam(value = "课程Id", required = true)
            @PathVariable String id) {

        List<Tests> testsList = testsService.selectTestsInfoById(id);
        return R.ok().data("testsList",testsList);
    }
//    @ApiOperation(value = "获取测试")
//    @GetMapping("getId/{id}")
//    public R getId(
//            @ApiParam(value = "课程Id", required = true)
//            @PathVariable String id) {
//
//        List<Tests> testsList = testsService.selectTestsById(id);
//        return R.ok().data("testsList",testsList);
//    }
    @ApiOperation("获取测试")
    @GetMapping("getId/{id}")
    public R getById(@ApiParam(value = "测试题id", required = true) @PathVariable String id) {
        Tests byId = testsService.getById(id);
        if (byId != null) {
            return R.ok().data("item", byId);
        } else {
            return R.error().message("数据不存在");
        }
    }


    @ApiOperation("根据ID删除测试")
    @DeleteMapping("remove/{id}")
    public R removeById(
            @ApiParam(value = "测试ID", required = true)
            @PathVariable String id){


        boolean result = testsService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }


    @ApiOperation("更新测试题目")
    @PutMapping("update-Tests-info")
    public R updateTestsInfoById(
            @ApiParam(value = "课程基本信息", required = true)
            @RequestBody Tests tests){

        testsService.updateById(tests);
        return R.ok().message("修改成功");
    }
    @ApiOperation("新增测试题")
    @PostMapping("save-Tests-info")
    public R saveCourseInfo(
            @ApiParam(value = "测试题基本信息", required = true)
            @RequestBody Tests tests){
        boolean save = testsService.save(tests);
        return R.ok().data("save", save).message("保存成功");
    }
    @ApiOperation("推荐类别分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit) {

        Page<Tests> pageParam = new Page<>(page, limit);
        IPage<Tests> pageModel = testsService.page(pageParam);
        List<Tests> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }




}

